package com.example.mediaplayerdemo.chart.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.example.mediaplayerdemo.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.List;

public class PieChartActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pie_chart);
        PieChart chart = findViewById(R.id.PieChart);

        List<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(18.5f, "Green"));
        entries.add(new PieEntry(26.7f, "Yellow"));
        entries.add(new PieEntry(24.0f, "Red"));
        entries.add(new PieEntry(30.8f, "Blue"));
        PieDataSet set = new PieDataSet(entries, "Election Results");
        List<Integer> pieColor = new ArrayList<>(); //color
        pieColor.add(Color.parseColor("#BFDD7B"));
        pieColor.add(Color.parseColor("#E3DCA1"));
        pieColor.add(Color.parseColor("#78EA5A"));
        pieColor.add(Color.parseColor("#EE80B6"));
        set.setColors(pieColor);
        set.setSliceSpace(5);   //间隔
//        set.setDrawValues(false);   //数值
        chart.setHoleRadius(0);  //r
        chart.setTransparentCircleRadius(0); //透明r
        chart.setUsePercentValues(true);
        chart.setDrawEntryLabels(false);
        PieData data = new PieData(set);
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        chart.setData(data);
        chart.invalidate();

        Legend legend = chart.getLegend();
        legend.setEnabled(true);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
        legend.setXOffset(90);
        legend.setFormSize(10);
        legend.setTextSize(5f);
    }
}